﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class EditCvDetails : System.Web.UI.Page
    {

        private Applicant _currentApplicant;

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentApplicant = Session["CurrentUser"] as Applicant;

            if (!IsPostBack)
            {
                LoadCvData();
            }          
        }

        protected void ButtonAddEducation_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AddEducationPage.aspx");
        }
        protected void ButtonAddExperience_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AddEducationPage.aspx");
        }
        protected void ButtonAddMerit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/AddEducationPage.aspx");
        }

        protected void ListViewInfoEdit_OnItemCommand(object sender, ListViewCommandEventArgs e)
        {
            EditInformation(e);
        }    
        protected void ListViewEducationEdit_OnItemCommand(object sender, ListViewCommandEventArgs e)
        {
            EditEducation(e);
        }
        protected void ListViewExperiencesEdit_OnItemCommand(object sender, ListViewCommandEventArgs e)
        {
            EditExperiences(e);
        }       
        protected void ListViewMeritsEdit_OnItemCommand(object sender, ListViewCommandEventArgs e)
        {
            EditMerits(e);
        }

        protected void ListViewEducationEdit_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            LoadCvData();
        }
        protected void ListViewExperiencesEdit_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            LoadCvData();
        }
        protected void ListViewMeritsEdit_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            LoadCvData();
        }

        private void LoadCvData()
        {
            var cvList = new List<Cv>();

            var currentUsersCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

            cvList.Add(currentUsersCv);

            ListViewInfoEdit.DataSource = cvList;
            ListViewInfoEdit.DataBind();

            var currentUserEducations = currentUsersCv.Educations.ToList();

            ListViewEducationEdit.DataSource = currentUserEducations;
            ListViewEducationEdit.DataBind();

            var currentUserExperiences = currentUsersCv.Experiences.ToList();

            ListViewExperiencesEdit.DataSource = currentUserExperiences;
            ListViewExperiencesEdit.DataBind();

            var currentUserMerit = currentUsersCv.Merits.ToList();

            ListViewMeritsEdit.DataSource = currentUserMerit;
            ListViewMeritsEdit.DataBind();
        }

        private void EditMerits(ListViewCommandEventArgs e)
        {
            int index = e.Item.DisplayIndex;
            if (e.CommandName == "Save")
            {
                var textBoxMerits = (TextBox)e.Item.FindControl("TextBoxMerits");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserMerits =
                    currentUserCv.Merits.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var updatedMerits = new Merits
                {
                    CvCvId = currentUserCv.CvId,
                    Desription = textBoxMerits.Text,
                    MeritId = currentUserMerits[index].MeritId
                };

                CvDB.UpdateMerits(updatedMerits);
            }

            if (e.CommandName == "Delete")
            {
                var textBoxMerits = (TextBox)e.Item.FindControl("TextBoxMerits");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserMerits =
                    currentUserCv.Merits.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var updatedMerits = new Merits
                {
                    CvCvId = currentUserCv.CvId,
                    Desription = textBoxMerits.Text,
                    MeritId = currentUserMerits[index].MeritId
                };

                CvDB.DeleteMerit(updatedMerits.MeritId);
            }
        }
        private void EditInformation(ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Save")
            {
                var textBoxMisc = (TextBox)e.Item.FindControl("TextBoxMisc");

                var textBoxInteresst = (TextBox)e.Item.FindControl("TextBoxIntresst");

                var textBoxPersonalQual = (TextBox)e.Item.FindControl("TextBoxPersonalQual");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                currentUserCv.Interests = textBoxInteresst.Text;
                currentUserCv.Miscellaneous = textBoxMisc.Text;
                currentUserCv.PersonalQualities = textBoxPersonalQual.Text;

                CvDB.UpdateCV(currentUserCv);
            }
        }
        private void EditEducation(ListViewCommandEventArgs e)
        {
            int index = e.Item.DisplayIndex;
            if (e.CommandName == "Save")
            {
                var textBoxEducation = (TextBox)e.Item.FindControl("TextBoxEducation");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserEducations =
                    currentUserCv.Educations.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var updatedEducation = new Educations
                {
                    CvCvId = currentUserCv.CvId,
                    Desription = textBoxEducation.Text,
                    EducationId = currentUserEducations[index].EducationId
                };

                CvDB.UpdateEducation(updatedEducation);
            }

            if (e.CommandName == "Delete")
            {
                var textBoxEducation = (TextBox)e.Item.FindControl("TextBoxEducation");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserEducations =
                    currentUserCv.Educations.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var educationToDelete = new Educations
                {
                    CvCvId = currentUserCv.CvId,
                    Desription = textBoxEducation.Text,
                    EducationId = currentUserEducations[index].EducationId
                };

                CvDB.DeleteEducation(educationToDelete.EducationId);
            }
        }
        private void EditExperiences(ListViewCommandEventArgs e)
        {
            int index = e.Item.DisplayIndex;
            if (e.CommandName == "Save")
            {
                var textBoxExperiences = (TextBox)e.Item.FindControl("TextBoxExperience");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserExperiences =
                    currentUserCv.Experiences.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var updatedExperiences = new Experiences
                {
                    CvCvId = currentUserCv.CvId,
                    Description = textBoxExperiences.Text,
                    ExperienceId = currentUserExperiences[index].ExperienceId
                };

                CvDB.UpdateExperience(updatedExperiences);
            }

            if (e.CommandName == "Delete")
            {
                var textBoxExperiences = (TextBox)e.Item.FindControl("TextBoxExperience");

                var currentUserCv = CvDB.GetCvInstanceWithApplicantId(_currentApplicant.ApplicantId);

                var currentUserExperiences =
                    currentUserCv.Experiences.Where(ed => ed.CvCvId == currentUserCv.CvId).ToList();

                var updatedExperiences = new Experiences
                {
                    CvCvId = currentUserCv.CvId,
                    Description = textBoxExperiences.Text,
                    ExperienceId = currentUserExperiences[index].ExperienceId
                };

                CvDB.DeleteExperience(updatedExperiences.ExperienceId);
            }
        }
    }
}